import { Injectable } from '@nestjs/common';
import { createTransport, Transporter } from 'nodemailer';
import { ConfigService } from '@nestjs/config';
@Injectable()
export class EmailService {

  transporter: Transporter

  constructor(private configService: ConfigService) {
    this.transporter = createTransport({
      host: this.configService.get('nodemailer_host'),
      port: this.configService.get('nodemailer_port'),
      secure: false,
      auth: {
        // user: '你的邮箱地址',
        // pass: '你的授权码'
        user: this.configService.get('nodemailer_user'),
        pass: this.configService.get('nodemailer_pass')
      },
    });
  }

  async sendMail({ to, subject, html }) {
    await this.transporter.sendMail({
      from: {
        name: '前端加油站',
        address: this.configService.get('nodemailer_user')
      },
      to,
      subject,
      html
    });
  }
}
